//
//  TravelViewController.m
//  BJS151148_ TeamProject_Travel
//
//  Created by lanou3g on 16/3/21.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TravelDetailTableViewController.h"
#import "TravelViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "CDHttpHelper.h"
#import "TavelView.h"
#import "LCViewCell.h"
#import "SKModel.h"

@interface TravelViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)TavelView *travelView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,assign)NSInteger page;

@end

@implementation TravelViewController
- (void)loadView{

    self.travelView = [[TavelView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.travelView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = 1;
    [self handleData];
    [self.travelView.tableView registerClass:[LCViewCell class] forCellReuseIdentifier:@"cell"];
    self.travelView.tableView.dataSource = self;
    self.travelView.tableView.delegate = self;

    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    
    /**下拉刷新,上拉加载*/
    [self.travelView.tableView addHeaderWithTarget:self action:@selector(headerAction)];
    [self.travelView.tableView addFooterWithTarget:self action:@selector(footerAction)];
    
}
/**下拉刷新*/
- (void)headerAction{

    if (self.page > 1) {
        self.page -= 1;
    }else{
        self.page = 1;
    }
    [self.dataArray removeAllObjects];
    self.page = 1;
    [self handleData];
    [self.travelView.tableView headerEndRefreshing];
}
/**上拉加载*/
- (void)footerAction{
    [self.dataArray removeAllObjects];
    [self handleData];
    self.page += 1;
    [self handleData];
    [self.travelView.tableView footerEndRefreshing];
}
/**数据源处理*/
- (void)handleData{

    self.dataArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"https://chanyouji.com/api/trips/featured.json?page=%ld",self.page];
    [[CDHttpHelper defaultCDHttpHelper]get:url params:nil success:^(id responseObj) {
        NSError *error = nil;
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:&error];
        
        for (NSDictionary *dic in array) {
            SKModel *model = [[SKModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.travelView.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 250;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LCViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if(self.dataArray.count != 0){
        
        SKModel *skmo = [[SKModel alloc]init];
        skmo = self.dataArray[indexPath.row];
        cell.aLabel.text = skmo.name;
        cell.cLabel.text = [NSString stringWithFormat:@"%ld图",skmo.photos_count];
        cell.bLabel.text = [NSString stringWithFormat:@"%@ / %ld天,",skmo.start_date,skmo.days];
        [cell.aimageView sd_setImageWithURL:[NSURL URLWithString:skmo.front_cover_photo_url]];
        [cell.bImageView sd_setImageWithURL:[NSURL URLWithString:skmo.user[@"image"]]];
<<<<<<< HEAD
        //为图片添加滤镜
//        CIImage *ciImage = [[CIImage alloc] initWithContentsOfURL:[NSURL URLWithString:skmo.front_cover_photo_url]];
//        CIFilter *filter = [CIFilter filterWithName:@"CISRGBToneCurveToLinear" keysAndValues:kCIInputImageKey,ciImage, nil];
//        [filter setDefaults];
        //获取绘制上下文
//        CIContext *context = [CIContext contextWithOptions:nil];
        
        //渲染并输出CIImage
//        CIImage *outputImage = [filter outputImage];
        
        //创建CGImage句柄
//        CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
        
        //获取图片
//        UIImage *image = [UIImage imageWithCGImage:cgImage];
        
        //释放CGImage句柄
//        CGImageRelease(cgImage);
        
//        cell.aimageView.image = image;
        
=======
//        //为图片添加滤镜
//        CIImage *ciImage = [[CIImage alloc] initWithContentsOfURL:[NSURL URLWithString:skmo.front_cover_photo_url]];
//        CIFilter *filter = [CIFilter filterWithName:@"CISRGBToneCurveToLinear" keysAndValues:kCIInputImageKey,ciImage, nil];
//        [filter setDefaults];
//        //获取绘制上下文
//        CIContext *context = [CIContext contextWithOptions:nil];
//        
//        //渲染并输出CIImage
//        CIImage *outputImage = [filter outputImage];
//        
//        //创建CGImage句柄
//        CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
//        
//        //获取图片
//        UIImage *image = [UIImage imageWithCGImage:cgImage];
//        
//        //释放CGImage句柄
//        CGImageRelease(cgImage);
//        
//        cell.aimageView.image = image;
>>>>>>> c124e849d1fd971cc4d7b94418139e1982da7978
    }
    return cell;
    
}
/**cell加载动画*/
////-(void)tableView:(UITableView *)tableView willDisplayCell:(LCViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    CATransform3D rotation;//3D旋转
//    
//    rotation = CATransform3DMakeTranslation(0 ,50 ,20);
//    rotation = CATransform3DScale(rotation, 0.9, .9, 1);
//    
//    rotation.m34 = 1.0/ -600;
//    
//    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//    cell.layer.shadowOffset = CGSizeMake(10, 10);
//    cell.alpha = 0;
//    
//    cell.layer.transform = rotation;
//    
//    [UIView beginAnimations:@"rotation" context:NULL];
//    //旋转时间
//    [UIView setAnimationDuration:0.6];
//    cell.layer.transform = CATransform3DIdentity;
//    cell.alpha = 1;
//    cell.layer.shadowOffset = CGSizeMake(0, 0);
//    [UIView commitAnimations];
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SKModel *skModel = self.dataArray[indexPath.row];
    TravelDetailTableViewController *travelDetailVC = [[TravelDetailTableViewController alloc]init];
    travelDetailVC.detailModel = skModel;
    [self.navigationController pushViewController:travelDetailVC animated:YES];
}
<<<<<<< HEAD

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
=======
>>>>>>> c124e849d1fd971cc4d7b94418139e1982da7978

@end
